package com.feinno.urcs.group.util;





import java.io.IOException;
import java.util.Base64;

/**
 * 没想好. 再看看
 *
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
public enum MSRPTextMsgEncodingType implements MSRPTextMsgEncodingParser {

	/**
	 * 普通的字符串
	 */
	DEFAULT("default", new MSRPTextMsgEncodingParser() {
		@Override
		public String getContent(MsrpContent msrpContent) throws IOException {
			return msrpContent.getContent();
		}
	}),
	/**
	 * 经过base64 以后的
	 */
	BASE64("BASE64", new MSRPTextMsgEncodingParser() {
		@Override
		public String getContent(MsrpContent msrpContent) throws IOException {
			if (msrpContent.getContent() == null) {
				throw new NullPointerException("base64decode args null");
			}
			byte[] b = Base64.getDecoder().decode(msrpContent.getContent());
			return new String(b);
		}
	});

	MSRPTextMsgEncodingType(String encodingType, MSRPTextMsgEncodingParser parser) {
		this.encodingType = encodingType.trim().toLowerCase();
		this.parser = parser;
	}

	private String encodingType;
	private MSRPTextMsgEncodingParser parser;

	@Override
	public String getContent(MsrpContent msrpContent) throws IOException {
		return this.parser.getContent(msrpContent);
	}

	public String getEncodingType() {
		return encodingType;
	}

	public void setEncodingType(String encodingType) {
		this.encodingType = encodingType;
	}
}

interface MSRPTextMsgEncodingParser {
	public String getContent(MsrpContent msrpContent) throws IOException;
}


